<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use backend\models\ClassifyForm;
use backend\models\Classify;

class ClassifyController extends Controller{
	public $layout = 'admin_main';
	// public function actionIndex(){
	// 	return $this->render('index');
	// }
	function init()
    {
        $session = Yii::$app->session;
        if(empty($session->get('admin_user')))
        {
            $this->redirect('?w=login/login');
        }
    }
	//分类添加页面
	public function actionClassify(){
		$class = new ClassifyForm;
		$classify = new Classify;
		$data=$classify->find()->where(['classify_action'=>0])->all();
		// print_r($data);die;
		if($data==array())
		{
			$datas[0]='顶级分类';
		}else{
			foreach($data as $k=>$v){
				$datas[0] = '顶级分类';
				$datas[$v['id']] = $v['classify_name'];
			}
		}
		// print_r($datas);die;
		return $this->render('classify',['class'=>$class,'datas'=>$datas]);
	}
	//分类添加
	public function actionAdd(){
		$data = Yii::$app->request->post();
		$model = new Classify;
		$model->classify_name = $data['classify_name'];
		// echo '<pre>';
		// print_r($data['ClassifyForm']['classify_action']);die;
		$model->classify_action = $data['ClassifyForm']['classify_action'];
		if($model->insert()){
			$this->redirect(['query']);
		}
	}
	//展示分类列表
	public function actionQuery(){
		$classify = new Classify;
		$data= Yii::$app->db->createCommand('SELECT * FROM classify order by classify_action asc')->queryAll();
            	// echo '<pre>';
            	// print_r($data);die;
		// $data =$classify->find()->all();
		$c=$this->get($data);
		// print_r($c);die;
		//循环把所属分类添加到data数组中方便于展示
		// foreach($data as $k=>$v){
		// 	for($i=0;$i<count($data);$i++){
		// 		if($v['classify_action']==0){
		// 			$data[$k]['class']='顶级分类';
		// 		}
		// 		if($data[$i]['classify_action']==$data[$k]['id'])
		// 		{
		// 			$data[$i]['class']=$data[$k]['classify_name'];
		// 		}
		// 	}
		// }
		return $this->render('query',['data'=>$c]);
	}
	//修改页面展示
	public function actionClassify_save(){
		$class = new ClassifyForm;
		$id = Yii::$app->request->get('id');
		$classify_action = Yii::$app->request->get('classify_action');
		$data = Yii::$app->db->createCommand("select * from classify where id=$id")->queryOne();
		// $co = Yii::$app->db->createCommand("select * from classify where id=$classify_action")->queryOne();
		$classify = new Classify;
		$da=$classify->find()->where(['classify_action'=>0])->all();
		// print_r($data);die;
		if($da==array())
		{
			$datas[0]='顶级分类';
		}else{
			foreach($da as $k=>$v){
				$datas[0] = '顶级分类';
				$datas[$v['id']] = $v['classify_name'];
			}
		}
		// print_r($datas);die();
		$class->classify_action=$classify_action;
		return $this->render('classify_save',['class'=>$class,'datas'=>$datas,'classify_name'=>$data['classify_name'],'id'=>$id]);
	}
	//修改
	public function actionSave(){
		$data = Yii::$app->request->post();
		// print_r($data['ClassifyForm']['classify_action']);die;
		$classify = Classify::findOne($data['id']);
		$classify->classify_action = $data['ClassifyForm']['classify_action'];
		$classify->classify_name = $data['classify_name'];
		if($classify->save()){
			$this->redirect(['query']);
		}
	}
	//删除
	public function actionDel(){
		$id = Yii::$app->request->get('id');
		$classify = Classify::findOne($id);
		if($classify->delete()){
			$this->redirect(['query']);
		}
	}
	//递归
	public function get($data,$pid=0)
	{
		global $tmp;
		foreach($data as $key=>$val)
		{
			if($val['classify_action']==$pid)
			{
				// $val['lavel'] = $lavel;
				$tmp[] = $val;
				$this->get($data,$val['id']);
			}
		}
		return $tmp;
	}
}
?>